public class Compare {

	private Operands op1, op2;
	private int operation;
	
	public Compare(Tokens tokens) {
		this.parse(tokens);
	}

	public void parse(Tokens tokens)  {
		// check for '('
		if(tokens.getToken() != 20){
			System.err.println("Comparisons must start with keyword '(' before token: " + tokens.idName());
			System.exit(0);
		}
		tokens.skipToken();
		this.op1 = new Operands(tokens);
		
		// check if token is a valid comparison operation
		if(tokens.getToken() < 25 || tokens.getToken() > 30){
			System.err.println("There must be a comparison operation before token: " + tokens.idName());
			System.exit(0);
		}
		this.operation = tokens.getToken();
		tokens.skipToken();
		this.op2 = new Operands(tokens);
		// check for ')'
		if(tokens.getToken() != 21){
			System.err.println("Comparisons must end with keyword ')' before token: " + tokens.idName());
			System.exit(0);
		}
		tokens.skipToken();
	}

	public boolean runComp() {
		boolean flag;
		if(this.operation == 25){ // operation is !=
			flag = this.op1.runOp() != this.op2.runOp();
		} else if(this.operation == 26){ // operation is ==
			flag = this.op1.runOp() == this.op2.runOp();
		} else if(this.operation ==  27){ // operation is <
			flag = this.op1.runOp() < this.op2.runOp();
		} else if(this.operation ==  28){ // operation is >
			flag = this.op1.runOp() > this.op2.runOp();
		} else if(this.operation ==  29) { // operation is <=
			flag = this.op1.runOp() <= this.op2.runOp();
		} else { // operation is >=
			flag = this.op1.runOp() >= this.op2.runOp();
		}
		return flag;
	}
}